package model

import (
	"gorm.io/gorm"
	"smart-health-platforms/config"
	"time"
)

type SeckillOrder struct {
	Id            uint           `gorm:"column:id;type:bigint UNSIGNED;comment:主键;primaryKey;not null;" json:"id"`                                                // 主键
	OrderSn       string         `gorm:"column:order_sn;type:varchar(22);comment:订单编号;not null;" json:"order_sn"`                                                 // 订单编号
	UserId        uint           `gorm:"column:user_id;type:bigint;comment:用户ID;not null;" json:"user_id"`                                                        // 用户ID
	SeckillGoodId uint           `gorm:"column:seckill_good_id;type:bigint;comment:秒杀商品id;not null;" json:"seckill_good_id"`                                      // 秒杀商品id
	SeckillPrice  float64        `gorm:"column:seckill_price;type:decimal(10, 2);comment:实际秒杀价格;not null;" json:"seckill_price"`                                  // 实际秒杀价格
	Quantity      uint           `gorm:"column:quantity;type:int;comment:购买数量;not null;" json:"quantity"`                                                         // 购买数量
	OrderStatus   uint           `gorm:"column:order_status;type:tinyint UNSIGNED;comment: 订单状态：1-未支付 2-已支付 3-已取消 4-已退款;not null;default:1;" json:"order_status"` //  订单状态：1-未支付 2-已支付 3-已取消 4-已退款
	CreatedAt     time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt     time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt     gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (sl *SeckillOrder) TableName() string {
	return "seckill_order"
}
func (sl *SeckillOrder) CreateSeckillOrder() error { //创建秒杀订单
	return config.DB.Create(&sl).Error
}
func (sl *SeckillOrder) GetSeckillOrderById() error { //查询订单
	return config.DB.Where("id = ?", sl.Id).Limit(1).Find(&sl).Error
}
func (sl *SeckillOrder) DeleteSeckillOrder() error { //删除订单
	return config.DB.Where("id = ?", sl.Id).Delete(&sl).Error
}
func (sl *SeckillOrder) UpdateSeckillOrderStatus() error { //根据订单编号查询订单
	return config.DB.Model(&SeckillOrder{}).Where("order_sn = ?", sl.OrderSn).Update("order_status", sl.OrderStatus).Error
}
